<!DOCTYPE html>
<html lang="en">
<head>
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="4oDu9FBIEUQY3tUyERR6kppEHHZi9LdJmulKg0v0wuU" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Fainle的博客">
    <meta name="keyword"  content="github">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        九：层次聚类（Hierarchical clustering） - Fainle的博客 | Fainle&#39;s Blog
        
    </title>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/aircloud.css">
    <link rel="stylesheet" href="/css/gitment.css">
    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</head>

<body>

<!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> 人生苦短，多思考，少编程 </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>Fainle</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archive">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#一-概述"><span class="toc-text">一 . 概述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#二-算法分类"><span class="toc-text">二 . 算法分类</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-SingleLinkage"><span class="toc-text">1. SingleLinkage</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-CompleteLinkage"><span class="toc-text">2. CompleteLinkage</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-AverageLinkage"><span class="toc-text">3. AverageLinkage</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#三-优缺点"><span class="toc-text">三 . 优缺点</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-优点"><span class="toc-text">1. 优点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-缺点"><span class="toc-text">2. 缺点</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#四-sklearn-示例"><span class="toc-text">四 . sklearn 示例</span></a></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> 人生苦短，多思考，少编程 </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        九：层次聚类（Hierarchical clustering）
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2020-08-06 10:00</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#机器学习" title="机器学习">机器学习</a>
        <span>/</span>
        
        <a class="tag" href="/tags/#无监督学习" title="无监督学习">无监督学习</a>
        <span>/</span>
        
        <a class="tag" href="/tags/#聚类" title="聚类">聚类</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content ">
        <h3 id="一-概述"><a href="#一-概述" class="headerlink" title="一 . 概述"></a>一 . 概述</h3><p>层次聚类是一种聚类分析方法，旨在构建聚类的层次结构。<br>对于层次聚类策略一般分为两种类型： </p>
<ol>
<li>自下而上：</li>
</ol>
<p>每个观测值都从其自己的聚类开始，随着层次结构的上升，聚类对合并在一起。</p>
<ol>
<li>自上而下：</li>
</ol>
<p>所有观测都从一个群集开始，并且随着层次向下移动，拆分将递归执行。</p>
<h3 id="二-算法分类"><a href="#二-算法分类" class="headerlink" title="二 . 算法分类"></a>二 . 算法分类</h3><h4 id="1-SingleLinkage"><a href="#1-SingleLinkage" class="headerlink" title="1. SingleLinkage"></a>1. SingleLinkage</h4><p>取两个类中最近的两个样本之间的距离作为两个集合的距离，即：最近的两个样本之间的距离越小，</p>
<h4 id="2-CompleteLinkage"><a href="#2-CompleteLinkage" class="headerlink" title="2. CompleteLinkage"></a>2. CompleteLinkage</h4><p>取两个集合距离最远的两个点的距离作为两个集合的距离，其效果也刚好相反，限制非常大。</p>
<h4 id="3-AverageLinkage"><a href="#3-AverageLinkage" class="headerlink" title="3. AverageLinkage"></a>3. AverageLinkage</h4><p>两个集合中的点两两距离全部放在一起求平均值，相应的能得到一点合适的结果。</p>
<h3 id="三-优缺点"><a href="#三-优缺点" class="headerlink" title="三 . 优缺点"></a>三 . 优缺点</h3><h4 id="1-优点"><a href="#1-优点" class="headerlink" title="1. 优点"></a>1. 优点</h4><ol>
<li>层次表达的结构信息丰富</li>
<li>数据容易可视化</li>
</ol>
<h4 id="2-缺点"><a href="#2-缺点" class="headerlink" title="2. 缺点"></a>2. 缺点</h4><ol>
<li>对噪音和离群值很敏感</li>
<li>计算量很大</li>
</ol>
<h3 id="四-sklearn-示例"><a href="#四-sklearn-示例" class="headerlink" title="四 . sklearn 示例"></a>四 . sklearn 示例</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn <span class="keyword">import</span> datasets, cluster</span><br><span class="line"><span class="keyword">from</span> scipy.cluster.hierarchy <span class="keyword">import</span> dendrogram, ward, single</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line">x = datasets.load_iris().data[:<span class="number">10</span>]</span><br><span class="line">clust = cluster.AgglomerativeClustering(n_clusters=<span class="number">2</span>, linkage=<span class="string">'ward'</span>)  <span class="comment"># ward = 离平方差</span></span><br><span class="line">labels = clust.fit_predict(x)</span><br><span class="line"></span><br><span class="line">linkage_matrix = ward(x)</span><br><span class="line">dendrogram(linkage_matrix)</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        
        <li>
            <a target="_blank"  href="https://github.com/Fainle">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-github"></i>
                            </span>
            </a>
        </li>
        

        
        <li>
            <a target="_blank"  href="https://www.linkedin.com/in/Fainle">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-linkedin"></i>
                            </span>
            </a>
        </li>
        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a href="https://fainle.github.io">fainle</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a href="https://hexo.io/">Hexo</a>  Theme <a href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer><!-- hexo-inject:begin --><!-- hexo-inject:end -->




</body>
<script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>

<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="/js/index.js"></script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


    <script>
        /**
         *  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
         *  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables
        */
        if( '' || '')
        var disqus_config = function () {
            this.page.url = '';  // Replace PAGE_URL with your page's canonical URL variable
            this.page.identifier = ''; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
        };

        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document, s = d.createElement('script');
            s.src = 'https://fainle.disqus.com/embed.js';
            s.setAttribute('data-timestamp', +new Date());
            (d.head || d.body).appendChild(s);
        })();
    </script>



</html>
